<?php
include_once "Network.php";

$network = new Network();
$network->connect();

$data = array();
$network->send("cmd=status_get\r\n");
while($from = $network->recv()) {
	$data[] = $from;
	if(strpos($from, "</response>") !== false) {
		break;
	}
}

echo "<!--\n";
echo "<pre>\n";
print_r($data);
echo "</pre>\n";
echo "-->\n";

//$response = new SimpleXMLElement($data);
$response = simplexml_load_string( implode("", $data) );

echo "<html>\n";
echo "<head>\n";
echo "<meta http-equiv='refresh' CONTENT='2;url=index.php'>\n";
echo "</head>\n";
echo "<body>\n";

// count some values before starting display
$count_task = 0;
$count_drones = 0;
$count_threads = 0;
$count_task = array();
$count_task[0] = $count_task[1] = $count_task[2] = 0;
foreach($response->tasklist->task as $task) {
	foreach($task as $key => $value) {
		switch($key) {
			case "id":
				break;
			case "state":
				switch($value) {
					case 0:
						$count_task[0] ++;
						break;
					case 1:
						$count_task[1] ++;
						break;
					case 2:
						$count_task[2] ++;
						break;
				}
				break;
		}
	}
}
foreach($response->dronelist->drone as $drone) {
	$count_drones ++;
	foreach($drone->block as $block) {
		foreach($block as $key => $value) {
			if(!strcmp($key, "threads")) {
				$count_threads += $value;
			}
		}
	}
}

echo "<h3>Server</h3>\n";
echo "<table>\n";
echo "<tr bgcolor='lightgray'>\n";
echo "\t<th>tasks pending\n";
echo "\t<th>tasks active\n";
echo "\t<th>tasks complete\n";
echo "<tr>\n";
echo "\t<td align='center'>". $count_task[0] ."\n";
echo "\t<td align='center'>". $count_task[1] ."\n";
echo "\t<td align='center'>". $count_task[2] ."\n";
echo "</table>\n";

echo "<h3>Tasks</h3>\n";
echo "<table>\n";
echo "<tr bgcolor='lightgray'>\n";
echo "<th>id\n";
echo "<th>state\n";
echo "<th>pending\n";
echo "<th>complete\n";
echo "<th>task progress\n";
foreach($response->tasklist->task as $task) {
	if($task->state == 1) {
		echo "<tr>\n";
		foreach($task as $key => $value) {
			echo "<td align='center'>$value\n";
		}
		echo "\t<td><progress" 
			." value='".  $task->complete ."'"
			." max='". $task->pending ."'"
			."></progress>\n";
	}
}
echo "</table>\n";

echo "<h3>Client</h3>\n";
echo "<table>\n";
echo "<tr bgcolor='lightgray'>\n";
echo "\t<th># drones\n";
echo "\t<th># threads\n";
echo "\t<th>cpu\n";
echo "<tr>\n";
echo "\t<td align='center'>". $count_drones ."\n";
echo "\t<td align='center'>". $count_threads ."\n";
echo "\t<td align='center'>". $response->cpu ."%\n";
echo "</table>\n";

echo "<h3>Blocks</h3>\n";
echo "<table>\n";
echo "<tr bgcolor='lightgray'>\n";
echo "\t<th>task_id\n";
echo "\t<th>index\n";
echo "\t<th>time stamp\n";
echo "\t<th>todo\n";
echo "\t<th>action\n";
echo "\t<th>threads\n";
echo "\t<th>block progress\n";
echo "\t<th>%\n";
echo "\t<th>eta\n";
echo "\t<th>pending\n";
echo "\t<th>active\n";
echo "\t<th>complete\n";
//echo "\t<th>total\n";
foreach($response->dronelist->drone as $drone) {
	foreach($drone->block as $block) {
		echo "<tr>\n";
		echo "\t<td align='center'>". $drone->task_id ."\n";
		echo "\t<td align='center'>". $block->index ."\n";
		echo "\t<td align='center'>"
			."&nbsp;"
			. date("m-d h:m", (int)$drone->timestamp)
			."&nbsp;"
		       	."\n";
		echo "\t<td align='center'>". $drone->todo ."\n";
		echo "\t<td align='center'>". $drone->action ."\n";
		echo "\t<td align='center'>". $block->threads ."\n";
		echo "\t<td><progress" 
			." value='".  $block->complete ."'"
			." max='". ($block->pending + $block->active + $block->complete) ."'"
			."></progress>\n";
		echo "\t<td>";
			printf("%d", round($block->complete / ($block->pending + $block->active + $block->complete) * 100));
			echo "%\n";
		echo "\t<td>";
		echo "\t<td align='center'>". $block->pending ."\n";
		echo "\t<td align='center'>". $block->active ."\n";
		echo "\t<td align='center'>". $block->complete ."\n";
//		echo "\t<td>". ($drone->pending + $drone->active + $drone->complete);
	}
}
echo "</table>\n";

/*
echo "<pre>\n";
print_r($data);
echo "</pre>\n";
*/

echo "</body>\n";
echo "</html>\n";

?>
